#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int son[N][26];
int cnt[N];
int index;
int n;
void insert(char str[])
{
	int p = 0;
	for (int i = 0; str[i]; i++) {
		int u = str[i] - 'a';
		if (!son[p][u]) {
			son[p][u] = ++index;
		}
		p = son[p][u];
	}
	cnt[p]++;	
}
int query(char str[])
{
	int p = 0;
	for (int i = 0; str[i]; i++) {
		int u = str[i] - 'a';
		if (!son[p][u])return 0;
		p = son[p][u];
	}
	return cnt[p];
}
int main()
{
	cin >> n;
	while (n--) {
		char op[2];
		cin >> op;
		if (op[0] == 'I') {
			char str[1010];
			cin >> str;
			insert(str);
		}
		else {
			char str[1010];
			cin >> str;
			cout << query(str) << endl;
		}
		
	}
	return 0;
}